package dataAccess;

import java.sql.*;
import java.util.*;

import dataAccess.Connector;
import dataAccess.DALException;
import dataAccess.PawnedFieldDTO;

public class MySQLPawnedFieldDAO implements PawnedFieldDAO{

	public PawnedFieldDTO getPawnedField(int PawnID) throws DALException {
		ResultSet rs = Connector.doQuery("SELECT * from pawnedfield WHERE Pawn_ID = "
				+ PawnID + ";"); 
		try {
			if (!rs.first())
				throw new DALException("Det pansatte felt " + PawnID
						+ " findes ikke");
			return new PawnedFieldDTO(rs.getInt("Pawn_ID"),
					rs.getInt("Field_ID"), rs.getInt("PawnPrice"), rs.getBoolean("IsPawned"));
		} catch (SQLException e) {
			throw new DALException(e);
		}
	}

	public List<PawnedFieldDTO> getPawnedFieldList() throws DALException {
		List<PawnedFieldDTO> data = new ArrayList<PawnedFieldDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM PawnedField");
		try {
			while (rs.next()) {
				data.add(new PawnedFieldDTO(rs.getInt("Pawn_ID"), rs
						.getInt("Field_ID"), rs.getInt("PawnPrice"), rs.getBoolean("IsPawned")));
			}
		} catch (SQLException e) {
			throw new DALException(e);
		}
		return data;
	}

	public void createPawnedField(PawnedFieldDTO pawnedField)
			throws DALException {
		Connector.doUpdate("INSERT INTO PawnedField VALUES ("
				+ pawnedField.getPawnID() + ", " + pawnedField.getFieldID()
				+ ", " + pawnedField.getPawnPrice() + ");");
	}

	public void updatePawnedField(PawnedFieldDTO pawnedField)
			throws DALException {
		Connector.doUpdate("UPDATE PawnedField SET Field_ID = "
				+ pawnedField.getFieldID() + ", PawnPrice = "
				+ pawnedField.getPawnPrice() + ", IsPawned = " + pawnedField.isPawned() + " WHERE Pawn_ID = "
				+ pawnedField.getPawnID() + ";");

	}
}
